package com.example.zhinongbao.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.example.zhinongbao.R;
import com.example.zhinongbao.model.Product;
import com.example.zhinongbao.provider.ZhinongbaoContract;
import com.example.zhinongbao.provider.ZhinongbaoProvider;
import com.example.zhinongbao.utils.CartManager;
import com.google.android.material.snackbar.Snackbar;

public class ProductDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        // 获取传递的商品数据
        Product product = (Product) getIntent().getSerializableExtra("product");

        // 保存足迹记录（重要修改）
        saveFootprint(product);

        // 初始化视图
        initViews(product);

        // 设置按钮点击事件
        setupButtonListeners();
    }

    // 保存浏览足迹
    private void saveFootprint(Product product) {
        ContentValues values = new ContentValues();
        values.put(ZhinongbaoContract.FootprintEntry.COLUMN_PRODUCT_ID, product.getId());
        values.put(ZhinongbaoContract.FootprintEntry.COLUMN_TIMESTAMP, System.currentTimeMillis());
        getContentResolver().insert(ZhinongbaoContract.FootprintEntry.CONTENT_URI, values);
    }

    private void initViews(Product product) {
        // 设置商品信息
        TextView tvPrice = findViewById(R.id.tv_price);
        ImageView ivProduct = findViewById(R.id.iv_product_main);
        TextView tvTitle = findViewById(R.id.tv_title);
        TextView tvShippingFrom = findViewById(R.id.tv_shipping_from);
        TextView tvShippingFee = findViewById(R.id.tv_shipping_fee);

        ivProduct.setImageResource(product.getImageResId());
        tvPrice.setText(String.format("¥%.1f", product.getPrice()));
        tvTitle.setText(product.getName());
        tvShippingFrom.setText("发货  " + product.getLocation());
        tvShippingFee.setText("物流  运费自理"); // 示例数据
    }

    private void setupButtonListeners() {
        Button btnAddCart = findViewById(R.id.btn_add_cart);
        Button btnBuyNow = findViewById(R.id.btn_buy_now);

        btnAddCart.setOnClickListener(v -> {
            // 获取当前商品
            Product product = (Product) getIntent().getSerializableExtra("product");
            if (product != null) {
                // 添加到购物车（默认数量1）
                CartManager.getInstance().addToCart(product, 1);

                // 显示添加成功提示
                Snackbar.make(findViewById(android.R.id.content),
                        "已添加到购物车", Snackbar.LENGTH_SHORT).show();
            }
        });

        btnBuyNow.setOnClickListener(v -> {
            // 立即购买逻辑
            Product product = (Product) getIntent().getSerializableExtra("product");
            if (product != null) {
                // 清空购物车后添加当前商品
                CartManager.getInstance().clearCart();
                CartManager.getInstance().addToCart(product, 1);

                // 跳转到结算页面
                startActivity(new Intent(this, CartActivity.class));
            }
        });
    }
}